package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.*;
import com.qf.pojo.*;
import com.qf.service.DtsGoodsAttributeService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGoodsSpecificationService;
import com.qf.service.DtsGoodsproductService;
import com.qf.vo.GoodsAllinone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/13 20:50
 */
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {
    @Autowired
    StatMapper statMapper;
    @Autowired
    DtsGoodsMapper goodsMapper;
    @Autowired
    DtsGoodsAttributeMapper attributeMapper;
    @Autowired
    DtsGoodsSpecificationMapper specificationMapper;
    @Autowired
    DtsGoodsProductMapper productMapper;
    @Autowired
    DtsCategoryMapper categoryMapper;
    @Autowired
    DtsGoodsproductService goodsProductService;
    @Autowired
    DtsGoodsSpecificationService goodsSpecificationService;
    @Autowired
    DtsGoodsAttributeService goodsAttributeService;

    @Override
    public long getTotal() {
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getDeleted,false);
        Integer count = goodsMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String goodsSn, String name, List<Integer> brandIds) {
        IPage<DtsGoods> iPage = new Page<>(page,limit);

        QueryWrapper<DtsGoods> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(goodsSn)){
            wrapper.like("`goods_sn`",goodsSn);
        }
        if(!StringUtils.isEmpty(name)){
            wrapper.like("`name`",name);
        }

        if (brandIds != null && brandIds.size() > 0){
            wrapper.in("`brands_id`",brandIds);
        }
        if ("desc".equalsIgnoreCase(order)) {
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }

        wrapper.eq("`deleted`",false);
        goodsMapper.selectPage(iPage,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }

    @Override
    public void insert(GoodsAllinone allinone) {

        DtsGoods goods = allinone.getGoods();

//        新增商品  判断商品是已经存在
        LambdaQueryWrapper<DtsGoods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoods::getGoodsSn,goods.getGoodsSn());
        Integer count = goodsMapper.selectCount(wrapper);
        if (count > 0){
            throw  new RuntimeException("商品编号已存在");
        }
        LambdaQueryWrapper<DtsGoods> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DtsGoods::getName,goods.getName());
        Integer count1 = goodsMapper.selectCount(wrapper1);
        if (count1 > 0){
            throw  new RuntimeException("商品名称已存在");
        }

//        新增商品
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);
        goodsMapper.insert(goods);

        DtsGoodsSpecification[] specifications = allinone.getSpecifications();
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            goodsSpecificationService.insert(specification);
        }
        DtsGoodsProduct[] products = allinone.getProducts();
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            goodsProductService.insert(product);
        }

        DtsGoodsAttribute[] attributes = allinone.getAttributes();
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            goodsAttributeService.insert(attribute);
        }
    }

    @Override
    public Map<String, Object> detail(Integer id) {
//        查询商品详情
        DtsGoods goods = goodsMapper.selectById(id);
//        查询商品属性
        LambdaQueryWrapper<DtsGoodsAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsGoodsAttribute::getDeleted,false);
        wrapper.eq(DtsGoodsAttribute::getGoodsId,goods.getId());
        List<DtsGoodsAttribute> attributes = attributeMapper.selectList(wrapper);

//        查询商品规格
        LambdaQueryWrapper<DtsGoodsSpecification> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DtsGoodsSpecification::getDeleted,false);
        wrapper1.eq(DtsGoodsSpecification::getGoodsId,goods.getId());
        List<DtsGoodsSpecification> specifications = specificationMapper.selectList(wrapper1);

//        查询商品类型
        LambdaQueryWrapper<DtsGoodsProduct> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(DtsGoodsProduct::getDeleted,false);
        wrapper2.eq(DtsGoodsProduct::getGoodsId,goods.getId());
        List<DtsGoodsProduct> products = productMapper.selectList(wrapper2);

//        查询商品分类
//        二级分类id
        Integer categoryId = goods.getCategoryId();
        DtsCategory category = categoryMapper.selectById(categoryId);
//        一级分类id
        Integer pid = category.getPid();


        Map<String, Object> data = new HashMap<>();
        data.put("categoryIds", Arrays.asList(pid,categoryId));
        data.put("goods", goods);
        data.put("attributes", attributes);
        data.put("specifications",specifications);
        data.put("products",products);

        return data;
    }

    @Override
    public void update(GoodsAllinone goodsAllinone) {

//        更新商品
        DtsGoods goods = goodsAllinone.getGoods();
        goods.setUpdateTime(new Date());
        goodsMapper.updateById(goods);

//        先删除再添加
//        更新商品库存
        goodsProductService.deleteByGoodsId(goods.getId());
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            product.setUpdateTime(new Date());
            productMapper.insert(product);
        }

//        更新商品属性
        goodsAttributeService.deleteByGoodsId(goods.getId());
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
            attribute.setGoodsId(goods.getId());
            attributeMapper.insert(attribute);
        }

//        更新商品规格
        goodsSpecificationService.deleteByGoodsId(goods.getId());
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        for (DtsGoodsSpecification specification : specifications) {
            specification.setUpdateTime(new Date());
            specification.setGoodsId(goods.getId());
            specificationMapper.insert(specification);
        }

    }

    @Override
    public void delete(DtsGoods goods) {
//        删除商品的属性
        attributeMapper.deleteById(goods.getId());
//        删除商品的规格
        specificationMapper.deleteById(goods.getId());
//        删除商品的库存
//        LambdaQueryWrapper<DtsGoodsProduct> productWrapper = new LambdaQueryWrapper<>();
//        productWrapper.eq(DtsGoodsProduct::getDeleted,true);
//        productWrapper.eq(DtsGoodsProduct::getGoodsId,goods.getId());
        productMapper.deleteById(goods.getId());
//        删除商品
//        LambdaQueryWrapper<DtsGoods> goodsWrapper = new LambdaQueryWrapper<>();
//        goodsWrapper.eq(DtsGoods::getDeleted,true);
//        goodsWrapper.eq(DtsGoods::getId,goods.getId());
//        goodsMapper.update(goods,goodsWrapper);
        goodsMapper.deleteById(goods.getId());
    }

    @Override
    public Map<String, Object> selectGoodsStat() {
        String[] columns = {"day", "orders", "products", "amount"};
        List<Map> rows = statMapper.findGoodsStat();
        Map<String, Object> map = new HashMap<>();
        map.put("columns",columns);
        map.put("rows",rows);
        return map;
    }
}
